package com.magus.project.yddemo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.annotation.CustomPagingParam;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.util.lang.StringUtils;
import com.magus.framework.core.vobean.PagingParam;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.framework.system.entity.Company;
import com.magus.framework.system.service.CompanyService;
import com.magus.framework.system.util.SecurityUtils;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.service.IndicatorDataService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 日报查看
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorDataDayManager", description = "日报查看")
@RestController
@RequestMapping("/yddemo/dataDays")
@AppGroupInfo(key = "yddemo:dataDay", name = "日报查看", appKey = "yddemo")
public class IndicatorDataDayController extends BaseController {

	@Autowired
	private IndicatorDataService indicatorDataService;

	@Autowired
	private CompanyService companyService;

	SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");

	@GetMapping("/findCompany")
	@RequiresPermissions("yddemo:dataDay:view")
	@ApiOperation(value = "根据当前登陆的用户找到关联的公司", notes = "根据当前登陆的用户找到关联的公司")
	ResponseEntity<Company> findCompany() {
		Company company = new Company();
		List<String> uniqueKeyList = SecurityUtils.findCompanies();
		if (uniqueKeyList != null && uniqueKeyList.size() > 0) {
			String uniqueKey = uniqueKeyList.get(0);
			company = companyService.findByUniqueKey(uniqueKey);
		}

		return ResponseEntity.ok(company);
	}

	@GetMapping("/getAllCompanys")
	@RequiresPermissions("yddemo:dataDay:view")
	@ApiOperation(value = "找到所有公司", notes = "找到所有公司")
	ResponseEntity<List<Company>> findAllCompany() {

		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("deleted", Boolean.FALSE));
		Specification<Company> spec = buildFilterRequest(searchFilters);
		List<Company> companyList = companyService.findAll(spec);

		return ResponseEntity.ok(companyList);
	}

	/**
	 * 查询日报
	 * 
	 * @param date
	 *            日期
	 * @param companyId
	 *            电厂
	 * @return 指标数据集合
	 */
	@GetMapping
	@RequiresPermissions("yddemo:dataDay:view")
	@ApiOperation(value = "查询日报", notes = "查询此电厂下面的日报")
	@AppFunctionInfo(key = "yddemo:dataDay:view", name = "查询日报列表", groupKey = "yddemo:dataDay", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/dataDays/page/view")
	ResponseEntity<Page<IndicatorData>> findAll(@RequestParam(required = false) String date,
			@RequestParam(required = false) String companyId, @CustomPagingParam PagingParam paginParam) {
		Date bDate = new Date();
		try {
			bDate = day.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		List<SearchFilter> searchFilters = new ArrayList<>();
		if (StringUtils.isNotBlank(companyId)) {
			searchFilters.add(SearchFilter.equal("indicatorDetail.company.id", companyId));
		}
		searchFilters.add(SearchFilter.equal("time", bDate.getTime()));
		searchFilters.add(SearchFilter.equal("dimension", "day"));
		searchFilters.add(SearchFilter.like("indicatorDetail.indicator.dimension", "day"));
		searchFilters.add(SearchFilter.like("indicatorDetail.dimension", "day"));

		paginParam.setSortProperty("time");
		paginParam.setSortDirection("asc");

		PageRequest request = buildPageRequest(paginParam);
		Specification<IndicatorData> spec = buildFilterRequest(searchFilters);
		// spec = DataAuthUtils.andDataAuthSpec(spec, IndicatorData.class);

		return ResponseEntity.ok(indicatorDataService.findAll(spec, request));
	}

}
